<template>
    <div class="app-container">
        <!-- 上部的form表单 -->
        <div class="top-form" style="text-align: center;">
                <el-form :inline="true" :model="queryParam" class="demo-form-inline">
                <el-form-item label="学科名 ">
                    <el-input v-model="queryParam.subjectName" placeholder="请输入学科名"
                    :clearable="true"></el-input>
                </el-form-item>
                <!-- <el-form-item label="所属年级">
                    <el-select v-model="queryParam.levelGrade" placeholder="所属年级" :clearable="true">
                    <el-option label="大一" :value="1"></el-option>
                    <el-option label="大二" :value="2"></el-option>
                    <el-option label="大三" :value="3"></el-option>
                    <el-option label="大四" :value="4"></el-option>
                    </el-select>
                </el-form-item> -->
                <el-form-item>
                    <el-button type="primary" round  icon="el-icon-search" @click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <router-link :to="{path: '/subject/edit' , query:{id: 0 ,msg:'add'}}" class="link-left">
                        <el-button type="primary" round  icon="el-icon-plus">新增</el-button>
                    </router-link>
                </el-form-item>
                <el-form-item style="margin-left: 90px;">
                    <el-button type="danger" round  icon="el-icon-delete" @click="handleDel">批量删除</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!-- 表格展示数据 -->
        <div class="main">
            <el-table
                ref="multipleTable"
                @selection-change="handleSelectionChange"
                :data="tableData"
                border
                style="width: 100%">
                <el-table-column
                    type="selection"
                    width="55">
                </el-table-column>
                <el-table-column
                    align="center"
                    label="序号"
                    type="index"
                    width="300">
                </el-table-column>
                <el-table-column
                    align="center"
                    prop="subjectName"
                    label="学科名"
                    width="500px">
                </el-table-column>
                <!-- 将年级删去 -->
                <!-- <el-table-column
                    align="center"
                    prop="levelName"
                    label="年级"
                    width="300px">
                </el-table-column> -->
                <el-table-column
                    prop="userStatus"
                    align="center"
                    label="操作"
                >
                <template slot-scope="scope">
                    <router-link :to="{path: '/subject/edit' , query:{id: scope.row.id,msg:'edit'}}" class="link-left">
                        <el-button type="primary" size="mini">编辑</el-button>
                    </router-link>
                    <el-button type="danger" size="mini" @click="handleDel(scope.row)">删除</el-button>
                </template>
                </el-table-column>
            </el-table>
            <!-- 分页器 -->
            <el-pagination
                style="textAlign:center;margin-top: 20px;"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParam.pageIndex"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="queryParam.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
        
    </div>
</template>

<script>
import { pageList ,delByIds}  from '@/api/subject/subject'
export default {
    name : 'subject',
    mounted(){
        this.search();
    },
    data(){
        return {
        queryParam: {
            subjectName: '',
            pageIndex: 1,
            pageSize: 10
            },
            listLoading: true,
            tableData:[],
            total: 0,
            multipleSelection: []
        }
    },
    methods:{
    async search(){
            let res = await pageList(this.queryParam);
            this.tableData = res.data.records;
            this.queryParam.pageIndex = res.data.current;
            this.queryParam.pageSize = res.data.size;
            this.total = res.data.total;
        },
        // 批量操作
        handleSelectionChange(val){
            val.forEach(element => {
                this.multipleSelection.push(element.id) 
            });
        },
        handleDel(row){
            let arr = [];
            if(row.id){
                arr.push(row.id);
                this.deleteByIds(arr);
            }else{
                if(this.multipleSelection.length <= 0){
                    this.$message.warning('请选择需要删除的数据')
                    return;
                }
                this.deleteByIds(this.multipleSelection);
                this.multipleSelection = [];
            }
        },
        deleteByIds(val){
            this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                delByIds(val).then(resp=>{
                    if(resp.code == 200){
                        this.$message({
                            type: 'success',
                            message: resp.message
                        });
                    }else{
                        this.$message({
                            type: 'error',
                            message: resp.message
                        });
                    }
                    this.search()
                })
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },

        // 分页相关
        handleSizeChange(val) {
            this.queryParam.pageSize = val;
            this.search();
        },
        handleCurrentChange(val) {
            this.queryParam.pageIndex = val;
            this.search();
        },

    }
}
</script>

<style scoped>
    .link-left{
        margin-right: 10px;
    }
</style>